﻿
using Models.Common;
using Models.Device;
using Models.TaskPlain;
using NetTaste;
using Newtonsoft.Json;
using UtilsORM;
using UtilsORM.MySqlData;
using static Models.Common.EnumDeviceState;
using UtilsBase;
using System.Data;
using GatewayServerService.CloudCenter;

namespace GatewayServerService.Device
{
    /// <summary>
    /// //普锐森社-环境监测仪
    /// </summary>
    public class ServiceDeviceRS4851040
    {
        ServiceCloudDeviceValueLog log = new ServiceCloudDeviceValueLog();
        //网关响应回调
        public void callBackUpdate(MQReqModel<object> httpReqModel)
        {
            try
            {
                ModelReceverData modelDeviceData = httpReqModel.toServerData;
                CommandLogModel commandLogModel = httpReqModel.toGatewayData;
                string result = modelDeviceData.commandResults[0].value;

                if (modelDeviceData.commandResults[0].value.Substring(0, 2) != commandLogModel.content.Substring(0, 2))
                {
                    return;
                }

                if (commandLogModel.key == "data")
                {
                    //一次查全部07 03 00 00 00 20 44 74
                    //07 03 40 00 19 00 1F 02 EE 00 FD 00 00 00 00 01 C1 07 0E 01 FF 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 C8 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 B1 71
                    if (result.Length != 206)
                    {
                        Console.WriteLine(result);
                        Console.WriteLine("数据长度不正确，无法解析");
                        return;
                    }

                    string dataHex0 = result.Substring(9, 5);//0
                    string dataHex1 = result.Substring(15, 5);//1
                    string dataHex2 = result.Substring(21, 5);//2
                    string dataHex3 = result.Substring(27, 5);//3
                    string dataHex4 = result.Substring(33, 5);//4
                    string dataHex5 = result.Substring(39, 5);//5
                    string dataHex6 = result.Substring(45, 5);//6
                    string dataHex7 = result.Substring(51, 5);//7
                    string dataHex8 = result.Substring(57, 5);//8
                    string dataHex9 = result.Substring(63, 5);//9
                    string dataHex10 = result.Substring(69, 5);//10-0a
                    string dataHex11 = result.Substring(75, 5);//11
                    string dataHex12 = result.Substring(81, 5);//12
                    string dataHex13 = result.Substring(87, 5);//13
                    string dataHex14 = result.Substring(93, 5);//14
                    string dataHex15 = result.Substring(99, 5);//15
                    string dataHex16 = result.Substring(105, 5);//16
                    string dataHex17 = result.Substring(111, 5);//17
                    string dataHex18 = result.Substring(117, 5);//18
                    string dataHex19 = result.Substring(123, 5);//19



                    int data0 = new UtilsDataTypeChange().hex16Convert10(dataHex0);
                    log.writeSensorlog(commandLogModel.d_guid, "pm2.5", data0.ToString(), "PM2.5", result);

                    int data1 = new UtilsDataTypeChange().hex16Convert10(dataHex1);
                    log.writeSensorlog(commandLogModel.d_guid, "pm10", data1.ToString(), "PM10", result);

                    double data2 = new UtilsDataTypeChange().hex16Convert10(dataHex2) * 0.1;
                    log.writeSensorlog(commandLogModel.d_guid, "humidity", UtilsNumber.GetNumberForLength(data2, 1).ToString(), "湿度", result);

                    // double data3 = new UtilsDataTypeChange().hex16Convert10(dataHex3) * 0.1;
                    string temperatureBinary = UtilsDataTypeChange.hexToBinaryTolStr(dataHex3);
                    double data3 = 0.0;
                    if (temperatureBinary.Substring(0, 1) == "0")
                    {
                        data3 = new UtilsDataTypeChange().hex16Convert10(dataHex3) * 0.1;
                    }
                    else if (temperatureBinary.Substring(0, 1) == "1")
                    {
                        //    
                        //"FF 9B" 转二进制1111111110011011
                        //1111111110011011 反码 
                        //0000000001100100
                        //补码最后一位加1
                        //0000000001100101
                        //=1100101
                        string bumabinary = UtilsByte.GetBuMaBinary(temperatureBinary);
                        data3 = UtilsDataTypeChange.binaryToDecimalStr(bumabinary) * -0.1;
                    }

                    log.writeSensorlog(commandLogModel.d_guid, "temperature", UtilsNumber.GetNumberForLength(data3, 1).ToString(), "温度", result);


                    int data6 = new UtilsDataTypeChange().hex16Convert10(dataHex6);
                    log.writeSensorlog(commandLogModel.d_guid, "light", data6.ToString(), "光照度", result);

                    int data7 = new UtilsDataTypeChange().hex16Convert10(dataHex7);
                    log.writeSensorlog(commandLogModel.d_guid, "tvoc", data7.ToString(), "TVOC", result);


                    int data8 = new UtilsDataTypeChange().hex16Convert10(dataHex8);
                    log.writeSensorlog(commandLogModel.d_guid, "co2", data8.ToString(), "二氧化碳", result);

                    double data9 = new UtilsDataTypeChange().hex16Convert10(dataHex9) * 0.01;
                    log.writeSensorlog(commandLogModel.d_guid, "formaldehyde", UtilsNumber.GetNumberForLength(data9, 2).ToString(), "甲醛", result);

                    double data10 = new UtilsDataTypeChange().hex16Convert10(dataHex10) * 0.01;
                    log.writeSensorlog(commandLogModel.d_guid, "ozone", UtilsNumber.GetNumberForLength(data10, 2).ToString(), "臭氧", result);

                    double data19 = new UtilsDataTypeChange().hex16Convert10(dataHex19) * 0.1;
                    log.writeSensorlog(commandLogModel.d_guid, "noise", UtilsNumber.GetNumberForLength(data19, 1).ToString(), "噪音", result);



                }
                else
                {
                    //01 03 02 00 00 B8 44 
                    if (result.Length != 20)
                    {
                        Console.WriteLine(result);
                        Console.WriteLine("数据长度不正确，无法解析");
                        return;
                    }
                    if (commandLogModel.key == "light")
                    {
                        string dataHex1 = result.Substring(9, 5);
                        int data = new UtilsDataTypeChange().hex16Convert10(dataHex1);
                        log.writeSensorlog(commandLogModel.d_guid, "light", data.ToString(), "光照度", result);
                    }
                    else if (commandLogModel.key == "humidity")
                    {
                        string dataHex1 = result.Substring(9, 5);
                        double data = new UtilsDataTypeChange().hex16Convert10(dataHex1) * 0.1;
                        log.writeSensorlog(commandLogModel.d_guid, "humidity", UtilsNumber.GetNumberForLength(data, 1).ToString(), "湿度", result);
                    }
                    else if (commandLogModel.key == "temperature")
                    {
                        string dataHex1 = result.Substring(9, 5);
                        double data = new UtilsDataTypeChange().hex16Convert10(dataHex1) * 0.1;
                        log.writeSensorlog(commandLogModel.d_guid, "temperature", UtilsNumber.GetNumberForLength(data, 1).ToString(), "温度", result);
                    }
                    else if (commandLogModel.key == "pm10")
                    {
                        string dataHex1 = result.Substring(9, 5);
                        int data = new UtilsDataTypeChange().hex16Convert10(dataHex1);
                        log.writeSensorlog(commandLogModel.d_guid, "pm10", data.ToString(), "PM10", result);
                    }
                    else if (commandLogModel.key == "pm2.5")
                    {
                        string dataHex1 = result.Substring(9, 5);
                        int data = new UtilsDataTypeChange().hex16Convert10(dataHex1);
                        log.writeSensorlog(commandLogModel.d_guid, "pm2.5", data.ToString(), "PM2.5", result);
                    }
                    else if (commandLogModel.key == "co2")
                    {
                        string dataHex1 = result.Substring(9, 5);
                        int data = new UtilsDataTypeChange().hex16Convert10(dataHex1);
                        log.writeSensorlog(commandLogModel.d_guid, "co2", data.ToString(), "二氧化碳", result);
                    }
                    else if (commandLogModel.key == "formaldehyde")
                    {
                        string dataHex1 = result.Substring(9, 5);
                        double data = new UtilsDataTypeChange().hex16Convert10(dataHex1) * 0.01;
                        log.writeSensorlog(commandLogModel.d_guid, "formaldehyde", UtilsNumber.GetNumberForLength(data, 2).ToString(), "甲醛", result);
                    }
                    else if (commandLogModel.key == "tvoc")
                    {
                        string dataHex1 = result.Substring(9, 5);
                        int data = new UtilsDataTypeChange().hex16Convert10(dataHex1);
                        log.writeSensorlog(commandLogModel.d_guid, "tvoc", data.ToString(), "TVOC", result);

                    }
                    else if (commandLogModel.key == "noise")
                    {
                        string dataHex1 = result.Substring(9, 5);
                        double data = new UtilsDataTypeChange().hex16Convert10(dataHex1) * 0.1;
                        log.writeSensorlog(commandLogModel.d_guid, "noise", UtilsNumber.GetNumberForLength(data, 1).ToString(), "噪音", result);
                    }
                    else if (commandLogModel.key == "ozone")
                    {
                        string dataHex1 = result.Substring(9, 5);
                        double data = new UtilsDataTypeChange().hex16Convert10(dataHex1) * 0.01;
                        log.writeSensorlog(commandLogModel.d_guid, "ozone", UtilsNumber.GetNumberForLength(data, 2).ToString(), "臭氧", result);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

    }

}
